<template>
  <Dialog v-model="visible" :title="dialogTitle" :width="500" align-center>
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="110px"
    >
      <el-form-item label="上级部门名称" prop="parentName">
        <el-input v-model="formData.parentName" disabled placeholder="请输入子部门名称" />
      </el-form-item>
      <el-form-item label="子部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入子部门名称" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" clearable placeholder="请选择状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="显示排序" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" controls-position="right" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup>
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import {ref,reactive} from "vue"
  import * as DeptApi from '@/api/system/dept'
  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗
  const visible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formData = ref({
    id: undefined,
    title: '',
    parentId: undefined,
    name: undefined,
  })
  const formRules = reactive({
    name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    sort: [{ required: true, message: '请输入显示排序', trigger: 'blur' }],
  })

  /** 打开弹窗 */
  const open = async (name, id) => {
    visible.value = true
    dialogTitle.value = `新增“${name}”子部门`
    resetForm()
    // 修改时，设置数据
    if (id) {
      formLoading.value = true
      try {
        let data = await DeptApi.getDept(id)
        if(data){
          formData.value.parentName = data.name
          formData.value.parentId = data.id
        }
        console.log(formData.value, '拿到相关详情数据。。');
      } finally {
        formLoading.value = false
      }
    }

  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const formRef = ref() // 表单 Ref
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    title: '',
    parentId: undefined,
    name: undefined,
  }
  formRef.value?.resetFields()
}

const getList = inject('getList', () => { }); // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value?.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    await DeptApi.createDept(data)
    message.success(t('common.createSuccess'))
    visible.value = false
    // 发送操作成功的事件
    getList()
  } finally {
    formLoading.value = false
  }
}
</script>
<style lang="scss" scoped>

</style>
